package com.endbd.purchase.purchase_order.service.impl;

import com.endbd.feignapi.model.purchase.*;
import com.endbd.feignapi.model.system.Job;
import com.endbd.feignapi.model.system.User;
import com.endbd.feignapi.model.warehouse.RkWarehouse;
import com.endbd.feignapi.util.PageModel;
import com.endbd.purchase.purchase_order.dao.PurchaseDao;
import com.endbd.purchase.purchase_order.model.*;
import com.endbd.purchase.purchase_order.service.PQueryService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Auther:Lang
 * @Date:2022-11-16
 * @Time:20:14
 * @Description:采购单关于查询的业务逻辑层
 */
@Service
@Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT)
public class QueryService implements PQueryService {
    @Autowired
    private PurchaseDao purchaseDao;

    /**
     * @param poc       查询条件
     * @param pageModel 分页查询条件(pageNum、pageSize)
     * @return
     */
    @Override
    public PageModel<CPurchase> queryPurchasePageByCondition(PurchaseOrderCondition poc, PageModel<CPurchase> pageModel) {
        int pageSize = pageModel.getPageSize(); //每页显示的条数
        int pageNum = pageModel.getPageNum(); //当前页数

        pageSize = pageSize <= 0 ? 8 : pageSize;
        pageNum = pageNum <= 1 ? 1 : pageNum;

        PageHelper.startPage(pageNum, pageSize);


        List<CPurchase> audit = purchaseDao.queryPurchasePageByCondition(poc);

        PageInfo<CPurchase> info = new PageInfo<>(audit);

        pageModel.setPageNum(info.getPageNum());
        pageModel.setPageSize(info.getPageSize());
        pageModel.setTotalCount((int) info.getTotal());
        pageModel.setTotalPage(info.getPages());
        pageModel.setData(audit);

        return pageModel;
    }

    /**
     * 审核、修改详情页面
     *
     * @param id
     * @return
     */
    @Override
    public PurchaseDetail queryPurchaseDetailById(String id) {

        PurchaseOrderCondition conn = new PurchaseOrderCondition();
        conn.setPurchaseId(id);
        //根据采购单编号查找，采购单信息、采购商品信息
        List<CPurchase> purchases = purchaseDao.queryPurchasePageByCondition(conn);
        List<PurchaseData> data = purchaseDao.queryPurchaseDetailById(id);

        //循环查出来的品牌id 、类型id、商品给PurchaseDetail里的集合赋
        for (PurchaseData datum : data) {
            OtherPurchaseCondition opc = new OtherPurchaseCondition();
            opc.setBrandId(datum.getBrandId());
            List<CType> types = queryTypeByBrandId(opc);

            opc.setTypeId(datum.getTypeId());
            List<CProduct> products = queryProductByTypeId(opc);

            Integer[] firmId = new Integer[]{datum.getFirmId()};
            List<CFirm> firms = purchaseDao.queryFirmByFirmId(firmId);
            List<Object> list = new ArrayList<>();
            list.add(types);
            list.add(products);
            list.add(firms);
            datum.setData(list);
        }


        PurchaseDetail detail = new PurchaseDetail();
        detail.setPurchase(purchases.get(0));
        detail.setPurchaseData(data);

        return detail;
    }


    /**
     * 将提交审核页面所需的数据一次性查出放在前端
     *
     * @return
     */
    public Map queryJobAndStaff() {
        //董事会 id=1 采购部 id=2
        int[] arr = new int[]{1, 2};
        //存放部门-职位
        HashMap<Integer, Map> hashMap = new HashMap<>();
        //循环查出 部门下的职位
        for (int i : arr) {
            List<Job> jobs = purchaseDao.queryJobByDeptId(i);
            //当职位名称为采购组员时去掉
            jobs.removeIf((re) -> "采购组员".equals(re.getJobName()));

            //存放职位-职员
            HashMap<String, List> staffMember = new HashMap<>();
            for (Job job : jobs) {
                List<User> staff = purchaseDao.queryStaffByJobId(job.getJobId());
                staffMember.put(job.getJobName(), staff);
            }
            hashMap.put(i, staffMember);
        }
        return hashMap;
    }

    /**
     * 查询品牌
     *
     * @param opc
     * @return
     */
    @Override
    public List<CBrand> queryBrand(OtherPurchaseCondition opc) {
        return purchaseDao.queryBrandByBrandId(opc);
    }

    /**
     * 查询类型
     *
     * @param opc
     * @return
     */
    @Override
    public List<CType> queryTypeByBrandId(OtherPurchaseCondition opc) {

        List<CType> types = new ArrayList<>();
        if (opc.getBrandId() != 0 && opc.getBrandId() != null) {
            types = purchaseDao.queryTypeByBrandId(opc);
        }
        return types;
    }

    /**
     * 查询商品--型号、单价、单位、厂商id
     *
     * @param opc
     * @return
     */
    @Override
    public List<CProduct> queryProductByTypeId(OtherPurchaseCondition opc) {

        return purchaseDao.queryProductOrFirmIdByTypeId(opc);

    }

    /**
     * 查询厂商
     *
     * @param opc
     * @return
     */
    @Override
    public List<CFirm> queryFirm(OtherPurchaseCondition opc) {
        //先调用queryProductOrFirmIdtByTypeId 获取厂商id
        List<CProduct> firmId = purchaseDao.queryProductOrFirmIdByTypeId(opc);
        //将firm_id 存进数组里
        Integer[] firmIds = new Integer[firmId.size()];
        for (int i = 0; i < firmId.size(); i++) {
            firmIds[i] = firmId.get(i).getFirmId();
        }
        List<CFirm> firms = new ArrayList<>();
        if (firmIds.length > 0) {
            firms = purchaseDao.queryFirmByFirmId(firmIds);
        }

        return firms;
    }
}
